package com.sne.airways.customer.controller;

import com.sne.airways.basecore.controller.BaseController;
import com.sne.airways.basecore.exceptions.AppException;
import com.sne.airways.customer.dto.TdCustDTO;
import com.sne.airways.customer.service.CustService;
import com.sne.airways.system.constants.Constant;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 12-12-22
 * Time: 下午4:36
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/customer")
public class CustController extends BaseController {

    @Autowired
    private CustService custService;

    @RequestMapping("/queryCustList")
    @ResponseBody
    public void queryCustList(int start,int limit,String custName,String custPhone,String custType){
        List<TdCustDTO> custDTOList = custService.queryCustList(start,limit,custName,custPhone,custType);
        int count = custService.queryCustCount(custName,custPhone,custType);
        renderPageJsonArray(custDTOList,count);
    }
    @RequestMapping("/custSave")
    @ResponseBody
    public void custSave(String opType,String custId,String custName, String custRIF, String custCode, String enName, String phone, String custType,
                         String hsCode, String EDICode, String fax, String tix, String email, String contract, String address,
                         String level, int limitContract, String status, String remark, int version){
        if(StringUtils.isBlank(opType)||StringUtils.isBlank(custName)||StringUtils.isBlank(custCode)||StringUtils.isBlank(phone)){
            throw new AppException("获取参数出错");
        }
        if(StringUtils.equals(Constant.ADD,opType)){
            if(StringUtils.isBlank(phone)){
                throw new AppException("电话不能为空");
            }
            custService.custSave(custName,custRIF,custCode,enName,phone,custType,hsCode,EDICode,fax,tix,email,contract,address,level,limitContract,status,remark,version);
        }else if(StringUtils.equals(Constant.EDIT,opType)){
            if(StringUtils.isBlank((custId))){
                throw new AppException("客户Id获取失败!");
            }
            custService.custUpdate(custId,custName,custRIF,custCode,enName,phone,custType,hsCode,EDICode,fax,tix,email,contract,address,level,limitContract,status,remark,version);
        }
        renderSuccess();
    }
    @RequestMapping("/custdel")
    @ResponseBody
    public void custDelete(String opType,String custId,int version){
        if(StringUtils.isBlank(opType)||StringUtils.isBlank(custId)){
            throw new AppException("获取参数出错");
        }
        if(StringUtils.equals(Constant.DELETE,opType)){
            if(StringUtils.isBlank((custId))){
                throw new AppException("客户Id获取失败!");
            }
            custService.custDel(custId,version);
        }
        renderSuccess();
    }
}
